package com.jhx.video.videomanager

import com.jhx.common.http.ApiManager
import com.jhx.common.http.HttpRequest
import com.jhx.common.http.HttpResponse
import com.jhx.common.http.ResponseCallback
import com.jhx.video.model.VideoArray
import com.jhx.video.service.VideoService
import retrofit2.Call

/**
 *
 * @author Jack 2018-03-07 19:44
 */
class VideoManagerPresenterImpl : Contract.StoreManagerPresenter() {
    private var mCall: Call<HttpResponse<VideoArray>>? = null
    private var mStiCall: Call<HttpResponse<Any>>? = null
    private var mDelCall: Call<HttpResponse<Any>>? = null

    override fun recycler() {
        mView = null
    }

    override fun getVideoList(pageIndex: Int, pageSize: Int, onStart: (() -> Unit)?, onComplete: (() -> Unit)?) {
        mCall = ApiManager.get().getApiService(VideoService::class.java)
                .getVideoList(pageIndex, pageSize)
        onStart?.invoke()
        HttpRequest.getRequest().autoRecycle(mView?.getContext())
                .start(mCall, object: ResponseCallback<VideoArray>() {
                    override fun onSuccess(url: String?, data: VideoArray?) {
                        onComplete?.invoke()
                        mView?.getListSuccess(data?.videos)
                    }

                    override fun onError(url: String?, errorCode: Int, errorMsg: String?) {
                        super.onError(url, errorCode, errorMsg)
                        onComplete?.invoke()
                    }
                })
    }

    override fun deleteVideo(id: String) {
        mDelCall = ApiManager.get().getApiService(VideoService::class.java)
                .deleteVideo(id)
        mView?.showLoading()
        HttpRequest.getRequest().autoRecycle(mView?.getContext())
                .start(mDelCall, object: ResponseCallback<Any>() {
                    override fun onSuccess(url: String?, data: Any?) {
                        mView?.hideLoading()
                        mView?.deleteVideoSuccess()
                    }

                    override fun onError(url: String?, errorCode: Int, errorMsg: String?) {
                        super.onError(url, errorCode, errorMsg)
                        mView?.hideLoading()
                    }
                })
    }

    override fun stickVideo(id: String, isTop: Int) {
        mStiCall = ApiManager.get().getApiService(VideoService::class.java)
                .stickVideo(id, isTop)
        HttpRequest.getRequest().autoRecycle(mView?.getContext())
                .start(mStiCall, object: ResponseCallback<Any>() {
                    override fun onSuccess(url: String?, data: Any?) {
                        mView?.hideLoading()
                        mView?.stickVideoSuccess()
                    }

                    override fun onError(url: String?, errorCode: Int, errorMsg: String?) {
                        super.onError(url, errorCode, errorMsg)
                        mView?.hideLoading()
                    }
                })
    }
}